package org.tloss.web.editor.popup.actions;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.tloss.web.editor.Utils;

public class GenerateDatasource extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		final IResource resource = Utils.getCurrentResource();
		Job job = new Job("Generate Spring JDBC") {
			protected IStatus run(IProgressMonitor monitor) {
				if (resource != null && resource.getType() == IResource.FILE) {
					IProject project =  resource.getProject();
					return org.tloss.web.editor.utils.Generator
							.generateDataSource(project, resource, monitor);
				}
				return Status.OK_STATUS;
			}
		};
		job.setPriority(Job.LONG);
		job.schedule();
		return null;
	}

}
